package com.lry.send;

import com.lry.bean.EaNotice;
import com.lry.bean.dingding.DingDingNotice;
import com.lry.bean.dingding.DingDingResult;
import com.lry.httpclient.DingdingHttpClient;
import com.lry.properties.DingDingProperties;
import com.lry.text.NoticeTextResolver;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class DingDingNoticeSendComponent<T extends EaNotice> implements INoticeSendComponent<T> {

    private final DingdingHttpClient httpClient;

    private final NoticeTextResolver<T> noticeResolver;

    private final DingDingProperties dingDingProperties;


    /**
     * @param httpClient
     * @param noticeResolver
     * @param dingDingProperties
     */
    public DingDingNoticeSendComponent(DingdingHttpClient httpClient, NoticeTextResolver<T> noticeResolver,
                                       DingDingProperties dingDingProperties) {
        this.httpClient = httpClient;
        this.noticeResolver = noticeResolver;
        this.dingDingProperties = dingDingProperties;
    }

    public DingdingHttpClient getHttpClient() {
        return httpClient;
    }

    @Override
    public void send(T exceptionNotice) {
        String noticeText = noticeResolver.resolve(exceptionNotice);
        DingDingNotice dingDingNotice = dingDingProperties.generateDingdingNotice(noticeText,
                exceptionNotice.getTitle());
        DingDingResult result = httpClient.doSend(dingDingNotice);
        log.debug(result.toString());
    }

}
